package gui.advancedView;

import gui.commons.Resources;
import gui.commons.RoundedPanel;
import gui.mainView.UIView;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Insets;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRootPane;

import org.eclipse.jdt.internal.compiler.ast.ThisReference;

public class UIAdvancedView extends JInternalFrame{

	private static UIAdvancedView INSTANCE = null;
	private UIAdvancedControler controler;
	private UIView parent;

	private JPanel volumePan = new RoundedPanel();
	private JPanel speedPan = new RoundedPanel();
	private JPanel forwardPan = new RoundedPanel();
	private JPanel chaptersPan = new RoundedPanel();
	private JPanel soundtracksPan = new RoundedPanel();
	private JPanel subtitlesPan = new RoundedPanel();

	private JLabel voltxt = new JLabel("Volume");
	private JLabel speedtxt = new JLabel("Speed");
	private JLabel forwardtxt = new JLabel("Forward/Backward");
	private JLabel chapttxt = new JLabel("Chapters");
	private JLabel soundtxt = new JLabel("Sound tracks");
	private JLabel subtxt = new JLabel("Subtitles");

	public JButton volMin;
	public JButton volPlus;
	public JButton slow;
	public JButton fast;
	public JButton minus600;
	public JButton minus30;
	public JButton plus30;
	public JButton plus600;
	public JButton previousChapt;
	public JButton nextChapt;
	public JButton previousSound;
	public JButton nextSound;
	public JButton previousSub;
	public JButton nextSub;
	public JButton onoff;


	public JButton close;

	private int x,y,a,b;
	private GridBagConstraints constraint;

	private JPanel contentPane;

	private UIAdvancedView(UIView parent){
		this.contentPane=new JPanel();
		this.parent=parent;
		this.controler = new UIAdvancedControler(this, this.parent);

		Dimension dim = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
		this.y = (int)dim.getHeight();
		this.x = (int)dim.getWidth();
		this.a = (int)(dim.width*0.35);
		this.b= (int)(dim.height*0.20);


		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setSize((int)(x*0.25),(int)(y*0.30));
		this.setLocation(a, b);
		this.contentPane.setOpaque(true);
		this.contentPane.setBackground(Color.BLACK);

		this.setContentPane(contentPane);
		
		//LAYOUT
		this.contentPane.setLayout(new GridBagLayout());

		//Fonts
		Font font = new Font("Arial", Font.PLAIN, (int)(x/70));

		//Cellule Fermer
		this.close = new JButton(new ImageIcon(Resources.delIcon));
		this.close.setBorder(null);
		this.close.setBackground(null);
		this.close.addActionListener(controler);
		Insets i = new Insets(-40, 0, 0, 0); // Haut gauche Bas Droite
		this.constraint = this.createConstraint(0,0,2,1,GridBagConstraints.FIRST_LINE_END,GridBagConstraints.NONE,i);
		this.contentPane.add(this.close,this.constraint);
		
		//Cellule Volume
		this.volumePan.setLayout(new GridBagLayout());
		this.voltxt.setFont(font);
		this.voltxt.setForeground(Color.white);
		i = new Insets(5, 5, 2, 5); // Haut gauche Bas Droite
		this.constraint = this.createConstraint(0,0,1,1,GridBagConstraints.CENTER,GridBagConstraints.HORIZONTAL,i);
		this.volumePan.add(voltxt,this.constraint);
		
		JPanel volPan = new JPanel();
		GridLayout gl =new GridLayout(1,2);
		gl.setHgap(10);
		volPan.setLayout(gl);
		volPan.setOpaque(true);
		volPan.setBackground(Color.BLACK);
		volPan.setBorder(null);
		this.volMin = new JButton(new ImageIcon(Resources.volMin));
		this.volMin.setBorder(null);
		this.volMin.setBackground(null);
		this.volMin.addActionListener(controler);
		volPan.add(this.volMin);
		this.volPlus = new JButton(new ImageIcon(Resources.volPlus));
		this.volPlus.setBorder(null);
		this.volPlus.setBackground(null);
		this.volPlus.addActionListener(controler);
		volPan.add(this.volPlus);
		this.volumePan.setBorder(null);
		this.volumePan.setBackground(Color.BLACK);
		i = new Insets(0, 5, 5, 5);
		this.constraint = this.createConstraint(0,1,1,1,GridBagConstraints.CENTER,GridBagConstraints.HORIZONTAL,i);
		this.volumePan.add(volPan,this.constraint);
		
		i = new Insets(2, 2, 2, 2); // Haut gauche Bas Droite
		this.constraint = this.createConstraint(0,1,1,1,GridBagConstraints.CENTER,GridBagConstraints.HORIZONTAL,i);
		//this.constraint.ipadx=10;
		//this.constraint.ipady=10;
		this.contentPane.add(this.volumePan,this.constraint);

		//Cellule Speed
		this.speedPan.setLayout(new GridBagLayout());
		this.speedtxt.setFont(font);
		this.speedtxt.setForeground(Color.white);
		i = new Insets(5, 5, 2, 5); // Haut gauche Bas Droite
		this.constraint = this.createConstraint(0,0,2,1,GridBagConstraints.CENTER,GridBagConstraints.HORIZONTAL,i);
		this.speedPan.add(speedtxt,this.constraint);
		slow = new JButton(new ImageIcon(Resources.slow));
		this.slow.setBorder(null);
		this.slow.setBackground(null);
		this.slow.addActionListener(controler);
		i = new Insets(0, 5, 5, 5);
		this.constraint = this.createConstraint(0,1,1,1,GridBagConstraints.LINE_END,GridBagConstraints.NONE,i);
		this.speedPan.add(slow,this.constraint);
		fast = new JButton(new ImageIcon(Resources.fast));
		this.fast.setBorder(null);
		this.fast.setBackground(null);
		this.fast.addActionListener(controler);
		this.constraint = this.createConstraint(1,1,1,1,GridBagConstraints.CENTER,GridBagConstraints.NONE,i);
		this.speedPan.add(fast,this.constraint);
		this.speedPan.setBorder(null);
		this.speedPan.setBackground(Color.BLACK);

		i = new Insets(2, 2, 2, 2); // Haut gauche Bas Droite
		this.constraint = this.createConstraint(0,2,1,1,GridBagConstraints.CENTER,GridBagConstraints.HORIZONTAL,i);
		this.contentPane.add(this.speedPan,this.constraint);

		//Cellule Forward/Backward
		this.forwardPan.setLayout(new GridBagLayout());
		this.forwardtxt.setFont(font);
		this.forwardtxt.setForeground(Color.white);
		i = new Insets(5, 5, 2, 5); // Haut gauche Bas Droite
		this.constraint = this.createConstraint(0,0,1,1,GridBagConstraints.CENTER,GridBagConstraints.HORIZONTAL,i);
		this.forwardPan.add(forwardtxt,this.constraint);
		
		JPanel pan = new JPanel();
		gl =new GridLayout(1,4);
		gl.setHgap(10);
		pan.setLayout(gl);
		pan.setOpaque(true);
		pan.setBackground(Color.black);
		pan.setBorder(null);
		minus600 = new JButton(new ImageIcon(Resources.minus600));
		this.minus600.setBorder(null);
		this.minus600.setBackground(null);
		this.minus600.addActionListener(controler);
		pan.add(this.minus600);
		minus30 = new JButton(new ImageIcon(Resources.minus30));
		this.minus30.setBorder(null);
		this.minus30.setBackground(null);
		this.minus30.addActionListener(controler);
		pan.add(this.minus30);
		plus30 = new JButton(new ImageIcon(Resources.plus30));
		this.plus30.setBorder(null);
		this.plus30.setBackground(null);
		this.plus30.addActionListener(controler);
		pan.add(this.plus30);
		plus600 = new JButton(new ImageIcon(Resources.plus600));
		this.plus600.setBorder(null);
		this.plus600.setBackground(null);
		this.plus600.addActionListener(controler);
		pan.add(this.plus600);
		i = new Insets(0, 5, 5, 5); // Haut gauche Bas Droite
		this.constraint = this.createConstraint(0,1,1,1,GridBagConstraints.CENTER,GridBagConstraints.NONE,i);
		this.forwardPan.add(pan,this.constraint);
		this.forwardPan.setBorder(null);
		this.forwardPan.setBackground(Color.BLACK);

		i = new Insets(2, 2, 2, 2); // Haut gauche Bas Droite
		this.constraint = this.createConstraint(0,3,1,1,GridBagConstraints.CENTER,GridBagConstraints.HORIZONTAL,i);
		this.contentPane.add(this.forwardPan,this.constraint);

		//Cellule Chapters
		this.chaptersPan.setLayout(new GridBagLayout());
		this.chapttxt.setFont(font);
		this.chapttxt.setForeground(Color.white);
		i = new Insets(5, 5, 2, 5); // Haut gauche Bas Droite
		this.constraint = this.createConstraint(0,0,1,1,GridBagConstraints.CENTER,GridBagConstraints.HORIZONTAL,i);
		this.chaptersPan.add(chapttxt,this.constraint);
		
		JPanel chaptPan = new JPanel();
		gl =new GridLayout(1,2);
		gl.setHgap(10);
		chaptPan.setLayout(gl);
		chaptPan.setOpaque(true);
		chaptPan.setBackground(Color.BLACK);
		chaptPan.setBorder(null);
		previousChapt = new JButton(new ImageIcon(Resources.previous));
		this.previousChapt.setBorder(null);
		this.previousChapt.setBackground(null);
		this.previousChapt.addActionListener(controler);
		chaptPan.add(this.previousChapt);
		nextChapt = new JButton(new ImageIcon(Resources.next));
		this.nextChapt.setBorder(null);
		this.nextChapt.setBackground(null);
		this.nextChapt.addActionListener(controler);
		chaptPan.add(this.nextChapt);
		this.chaptersPan.setBorder(null);
		this.chaptersPan.setBackground(Color.BLACK);
		i = new Insets(0, 5, 5, 5); // Haut gauche Bas Droite
		this.constraint = this.createConstraint(0,1,1,1,GridBagConstraints.CENTER,GridBagConstraints.HORIZONTAL,i);
		this.chaptersPan.add(chaptPan,this.constraint);

		i = new Insets(2, 2, 2, 2); // Haut gauche Bas Droite
		this.constraint = this.createConstraint(1,1,1,1,GridBagConstraints.CENTER,GridBagConstraints.HORIZONTAL,i);
		this.contentPane.add(this.chaptersPan,this.constraint);

		//Cellule Sound tracks
		this.soundtracksPan.setLayout(new GridBagLayout());
		this.soundtxt.setFont(font);
		this.soundtxt.setForeground(Color.white);
		i = new Insets(5, 5, 2, 5); // Haut gauche Bas Droite
		this.constraint = this.createConstraint(0,0,1,1,GridBagConstraints.CENTER,GridBagConstraints.HORIZONTAL,i);
		this.soundtracksPan.add(soundtxt,this.constraint);
		
		JPanel soundPan = new JPanel();
		gl =new GridLayout(1,2);
		gl.setHgap(10);
		soundPan.setLayout(gl);
		soundPan.setOpaque(true);
		soundPan.setBackground(Color.BLACK);
		soundPan.setBorder(null);
		previousSound = new JButton(new ImageIcon(Resources.previous));
		this.previousSound.setBorder(null);
		this.previousSound.setBackground(null);
		this.previousSound.addActionListener(controler);
		soundPan.add(this.previousSound);
		nextSound = new JButton(new ImageIcon(Resources.next));
		this.nextSound.setBorder(null);
		this.nextSound.setBackground(null);
		this.nextSound.addActionListener(controler);
		soundPan.add(this.nextSound);
		this.soundtracksPan.setBorder(null);
		this.soundtracksPan.setBackground(Color.BLACK);
		i = new Insets(0, 5, 5, 5); // Haut gauche Bas Droite
		this.constraint = this.createConstraint(0,1,1,1,GridBagConstraints.CENTER,GridBagConstraints.HORIZONTAL,i);
		this.soundtracksPan.add(soundPan,this.constraint);

		i = new Insets(2, 2, 2, 2); // Haut gauche Bas Droite
		this.constraint = this.createConstraint(1,2,1,1,GridBagConstraints.CENTER,GridBagConstraints.HORIZONTAL,i);
		this.contentPane.add(this.soundtracksPan,this.constraint);
		
		//Cellule Subtitles
		this.subtitlesPan.setLayout(new GridBagLayout());
		this.subtxt.setFont(font);
		this.subtxt.setForeground(Color.white);
		i = new Insets(5, 5, 2, 5); // Haut gauche Bas Droite
		this.constraint = this.createConstraint(0,0,3,1,GridBagConstraints.CENTER,GridBagConstraints.NONE,i);
		this.subtitlesPan.add(subtxt,this.constraint);
		previousSub = new JButton(new ImageIcon(Resources.previous));
		this.previousSub.setBorder(null);
		this.previousSub.setBackground(null);
		this.previousSub.addActionListener(controler);
		i = new Insets(0, 5, 5, 5);
		this.constraint = this.createConstraint(0,1,1,1,GridBagConstraints.CENTER,GridBagConstraints.NONE,i);
		this.subtitlesPan.add(previousSub,this.constraint);
		onoff = new JButton(new ImageIcon(Resources.onoff));
		this.onoff.setBorder(null);
		this.onoff.setBackground(null);
		this.onoff.addActionListener(controler);
		i = new Insets(0, 5, 5, 5);
		this.constraint = this.createConstraint(1,1,1,1,GridBagConstraints.CENTER,GridBagConstraints.NONE,i);
		this.subtitlesPan.add(onoff,this.constraint);
		nextSub = new JButton(new ImageIcon(Resources.next));
		this.nextSub.setBorder(null);
		this.nextSub.setBackground(null);
		this.nextSub.addActionListener(controler);
		i = new Insets(0, 5, 5, 5);
		this.constraint = this.createConstraint(2,1,1,1,GridBagConstraints.CENTER,GridBagConstraints.NONE,i);
		this.subtitlesPan.add(nextSub,this.constraint);
		this.subtitlesPan.setBorder(null);
		this.subtitlesPan.setBackground(Color.BLACK);
		
		i = new Insets(2, 2, 2, 2); // Haut gauche Bas Droite
		this.constraint = this.createConstraint(1,3,1,1,GridBagConstraints.CENTER,GridBagConstraints.HORIZONTAL,i);
		this.contentPane.add(this.subtitlesPan,this.constraint);
		
		//this.contentPane.validate();
		//this.repaint();

		this.setTitle("AdvancedControls");
		
		this.putClientProperty("JInternalFrame.isPalette", Boolean.TRUE);
		this.getRootPane().setWindowDecorationStyle(JRootPane.NONE);
		this.setBorder(null);
		this.setResizable(false);
		this.setVisible(true);
	}

	public static synchronized UIAdvancedView getInstance(UIView parent)
	{	
		if (INSTANCE == null){ 
			INSTANCE = new UIAdvancedView(parent);
		}
		return INSTANCE;
	}

	public GridBagConstraints createConstraint(int gx, int gy, int gw, int gh, int anchor, int fill, Insets insets){
		GridBagConstraints gbc=new GridBagConstraints();
		gbc.gridx=gx;	//colonne
		gbc.gridy=gy;	//ligne
		gbc.gridwidth = gw;	//nb de colonnes occupées
		gbc.gridheight = gh;	//nb de lignes occupées
		gbc.anchor=anchor;	//position du composant dans la cellule
		gbc.fill = fill;	//redimensionnement du composant
		gbc.insets = insets;	//marges autour du composant 
		return gbc;
	}

	public synchronized void kill(){
		parent.advancedOpened=false;
		this.dispose();
		this.INSTANCE=null;
	}
}
